import { useModel } from "umi";
import compress from "gql-compress";
import { useReactive } from "ahooks";
import { v4 as uuidv4 } from "uuid";

const useCreateStore = () => {
    const { graphqlFetch } = useModel("global");
    const { baseInfo } = useModel("person.info");

    const state = useReactive({
        submitLoading: false,
    });

    const createStore = async (values) => {
        state.submitLoading = true;

        let variables = {
            oiUuid: uuidv4(),
            oiName: values.oiName.trim(),
            oiType: values.oiType,
            maInfo: values.maInfo || {},
            asInfo: values.asInfo || {},
            crInfo: { uuid: baseInfo.uuid, name: baseInfo.realName, time: new Date().getUTCTime() },
            oiPhon: values.oiPhon?.trim() || null,
            oiComm: values.oiComm?.trim() || null,
            oiEsta: values.oiEsta?.trim() || null,
            oiAddr: values.oiAddr?.trim() || null,
            oiDesc: values.oiDesc?.trim() || null,
            mpInfo: { radius: values.radius?.trim() || "", latitude: values.latitude?.trim() || "" },
            oiZoja: values.oiZoja || [],
            pyName: pinyin(values.oiName.trim(), { toneType: "none", nonZh: "consecutive" }),
            ptInfo: values.ptInfo || {},
        };

        if (variables.oiZoja && variables.oiZoja.length > 0) {
            variables.oiPrin = values.oiZoja[0].text;
            variables.oiCity = values.oiZoja[1].text;
            variables.oiDist = values.oiZoja[2].text;
            variables.oiStre = values.oiZoja[3].text;
        }

        let res = await graphqlFetch({
            query,
            variables,
        });

        state.submitLoading = false;

        return res;
    };

    return { createStore };
};

const query = compress(`mutation MyMutation(
    $oiUuid: UUID!
    $oiName: String!
    $oiDesc: String
    $maInfo: JSON
    $crInfo: JSON!
    $oiType: String!
    $pyName: String!
    $asInfo: JSON
    $mpInfo: JSON
    $oiAddr: String
    $oiCity: String
    $oiComm: String
    $oiDist: String
    $oiEsta: String
    $oiPhon: String
    $oiPrin: String
    $oiRoot: UUID
    $oiStre: String
    $oiZoja: JSON
    $ptInfo: JSON
) {
    createOrganizationInstitution(
        input: {
            organizationInstitution: {
                oiName: $oiName
                oiUuid: $oiUuid
                oiDesc: $oiDesc
                maInfo: $maInfo
                crInfo: $crInfo
                oiType: $oiType
                pyName: $pyName
                asInfo: $asInfo
                mpInfo: $mpInfo
                oiAddr: $oiAddr
                oiCity: $oiCity
                oiComm: $oiComm
                oiDist: $oiDist
                oiEsta: $oiEsta
                oiPhon: $oiPhon
                oiPrin: $oiPrin
                oiRoot: $oiRoot
                oiStre: $oiStre
                oiZoja: $oiZoja
                ptInfo: $ptInfo
            }
        }
    ) {
        clientMutationId
    }
}`);

export default useCreateStore;
